package com.menker.app.common;

import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.menker.app.R;
import com.menker.app.biz.PageInfo;
import com.menker.app.ui.main.MainActivity_;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.List;


/**
 * @author J.Huang
 * @version 1.0
 */
public abstract class BaseToolbarFragment extends Fragment implements ToolbarOwner.IToolbar {

    protected Toolbar mToolbar;
    protected TextView mToolbarTitle;
    protected View mToolbarBottomLine;
    protected RefreshLayout mRefreshLayout;      // 下拉刷新
    protected ToolbarOwner mToolbarOwner;
    protected List<ToolbarOwner.MenuAction> mActionBarMenuActionList;

    abstract protected void findViews();

    protected void setupUI(final PageInfo pageInfo, List<ToolbarOwner.MenuAction> menuActionList) {

        if (pageInfo == null) {
            return;
        }

        findViews();

        setHasOptionsMenu(true);
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);

        mToolbarOwner = new ToolbarOwner(this);

        if (!pageInfo.toolbarVisible) {
            mToolbar.setVisibility(View.GONE);
        } else {
            mToolbar.setVisibility(View.VISIBLE);

            mToolbarBottomLine.setVisibility(View.VISIBLE);
//            mToolbarBottomLine.setVisibility(View.GONE);

            ToolbarOwner.Config config = new ToolbarOwner.Config(pageInfo.toolbarTitle,
                    pageInfo.toolbarUpButtonEnabled,
                    menuActionList,
                    pageInfo.toolbarTitleColor, pageInfo.toolbarBgColor);
            mToolbarOwner.setConfig(config);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mToolbar = null;
        mToolbarTitle = null;
        mToolbarOwner = null;

        if (mActionBarMenuActionList != null) {
            mActionBarMenuActionList.clear();
        }

        super.onDestroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (!hidden) {
            ((MainActivity_) getActivity()).refreshCurrentTabIndex();
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (mToolbar != null) {
            Menu toolbarMenu = mToolbar.getMenu();
            toolbarMenu.clear();
            if (mActionBarMenuActionList != null && mActionBarMenuActionList.size() > 0) {
                for (final ToolbarOwner.MenuAction menuAction : mActionBarMenuActionList) {
                    toolbarMenu.add(menuAction.title)
                            .setIcon(menuAction.icon)
                            .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS)
                            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                                @Override
                                public boolean onMenuItemClick(MenuItem item) {
                                    menuAction.action.call();
                                    return true;
                                }
                            });
                }
            }
        }
//        inflater.inflate(R.menu.menu, menu);
//        mToolbar.inflateMenu(R.menu.menu);
//        mToolbar.setMenu();
//        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if (item.getItemId() == android.R.id.home) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setToolbarTitle(CharSequence title) {
        if (getActivity() != null
                && ((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            // 默认title居左，不使用
            ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("");
            if (mToolbarTitle != null) {
                mToolbarTitle.setText(title);
            }
        }
    }

    /***
     * 使左上角程序图标是否显示，如果设成false，则没有程序图标，仅仅就个标题，否则，显示应用程序图标
     * 对应id为android.R.id.home，对应ActionBar.DISPLAY_SHOW_HOME
     * @param enabled
     */
    @Override
    public void setDisplayShowHomeEnabled(boolean enabled) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
        }
    }

    /***
     *  给左上角图标的左边加上一个返回的图标。对应ActionBar.DISPLAY_HOME_AS_UP
     * @param enabled
     */
    @Override
    public void setUpButtonEnabled(boolean enabled) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            // setHomeButtonEnabled和setDisplayShowHomeEnabled共同起作用。
            // 如果setHomeButtonEnabled设成false，
            // 即使setDisplayShowHomeEnabled设成true，图标也不能点击
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(enabled);
            // setHomeButtonEnabled 小于4.0版本的默认值为true; 但是在4.0及其以上是false
            // 该方法的作用：决定左上角的图标是否可以点击。没有向左的小图标。true: 图标可以点击; false: 不可以点击。
            ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(enabled);
        }
    }

    @Override
    public void setNavigationIcon(int resId) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null && resId != 0) {
            mToolbar.setNavigationIcon(null);
        }
    }

    @Override
    public void setMenu(List<ToolbarOwner.MenuAction> menuActionList) {
        if (menuActionList != mActionBarMenuActionList) {
            mActionBarMenuActionList = menuActionList;
            getActivity().invalidateOptionsMenu();
            getActivity().supportInvalidateOptionsMenu();
        }
    }

    @Override
    public void setToolbarVisible(boolean visible) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            if (visible) {
                ((AppCompatActivity) getActivity()).getSupportActionBar().show();
            } else {
                ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
            }
        }

//        if (visible) {
//            mToolbar.setVisibility(View.VISIBLE);
//        } else {
//            mToolbar.setVisibility(View.GONE);
//        }
    }

    @Override
    public void setToolbarBgColor(int bgColor) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
//            mToolbar.setBackgroundColor(toolbarBgColor);

            if (bgColor == 0) {
                ((AppCompatActivity) getActivity()).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.colorPrimary)));
            } else {
                ((AppCompatActivity) getActivity()).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(bgColor));
            }
        }
    }

    @Override
    public void setToolbarTitleColor(int titleColor) {
        if (((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            if (titleColor == 0) {
                mToolbarTitle.setTextColor(ContextCompat.getColor(getActivity(), R.color.toolbar_title));
            } else {
                mToolbarTitle.setTextColor(titleColor);
            }
        }
    }

    public void setEnableRefresh(boolean enable) {
        if (mRefreshLayout == null) return;

        mRefreshLayout.setEnableRefresh(enable);
    }
}
